package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.query.CustomerReportQueryObject;
import cn.wolfcode.crm.service.ICustomerReportService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {

    @Autowired
    private ICustomerReportService customerReportService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"潜在客户报表列表权限", "customerReport:list"}, logical = Logical.OR)
    public String list(@ModelAttribute("qo") CustomerReportQueryObject qo, Model model) {
        List<Map<String, Object>> list = customerReportService.selectByQo(qo);
        model.addAttribute("list", list);
        return "customerReport/list";
    }

    @RequestMapping("/chartByBar")
    @RequiresPermissions(value = {"潜在客户报表列表权限", "customerReport:chartByBar"}, logical = Logical.OR)
    public String chartByBar(@ModelAttribute("qo") CustomerReportQueryObject qo, Model model) {
        try {
            String key  = new String(qo.getKey().getBytes("iso8859-1"),"utf-8");
            qo.setKeyword(key);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        //查询数据
        List<Map<String, Object>> maps = customerReportService.selectByQo(qo);

        List<String> sellers = new ArrayList<>();
        List<Long> customerNums = new ArrayList<>();

        for (Map<String, Object> map : maps) {
            //将分组类型遍历出来,
            String groupType = (String) map.get("groupType");
            Long groupNum = (Long) map.get("groupNum");

            sellers.add(groupType);
            customerNums.add(groupNum);

        }
        try {
            ObjectMapper mapper = new ObjectMapper();
            //销售人员信息
            model.addAttribute("sellers", mapper.writeValueAsString(sellers));
            //客户数量
            model.addAttribute("customerNums", mapper.writeValueAsString(customerNums));
            //分组类型
            model.addAttribute("groupType",qo.getGroupType());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "customerReport/chartByBar";
    }



    @RequestMapping("/chartByPie")
    @RequiresPermissions(value = {"潜在客户报表列表权限", "customerReport:chartByPie"}, logical = Logical.OR)
    public String chartByPie(@ModelAttribute("qo") CustomerReportQueryObject qo, Model model) {
        try {
            String key  = new String(qo.getKey().getBytes("iso8859-1"),"utf-8");
            qo.setKeyword(key);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //查询数据
        List<Map<String, Object>> maps = customerReportService.selectByQo(qo);

        List<String> sellers = new ArrayList<>();
        List<Map<String, Object>> customers = new ArrayList<>();

        for (Map<String, Object> map : maps) {
            String groupType = (String) map.get("groupType");
            sellers.add(groupType);
            Map<String, Object> customer = new HashMap<>();
            customer.put("value", map.get("groupNum"));
            customer.put("name", map.get("groupType"));
            customers.add(customer);
        }
        ObjectMapper mapper = new ObjectMapper();
        try {
            //销售人员信息
            model.addAttribute("sellers", mapper.writeValueAsString(sellers));
            //客户
            model.addAttribute("customers", mapper.writeValueAsString(customers));
            //分组类型
            model.addAttribute("groupType",qo.getGroupType());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return "/customerReport/chartByPie";

    }


}
